import axios from 'axios'

// 自定义axios
const myaxios = axios.create({
  baseURL: 'http://localhost:3000/api',
  timeout: 10000
})

// 拦截请求,携带token数据
myaxios.interceptors.request.use(function (config) {
  config.headers.token = localStorage.getItem('token')
  // console.log(config)
  return config
}, function (error) {
  // Do something with request error
  return Promise.reject(error)
})

// 拦截响应,登录守卫
myaxios.interceptors.response.use(function (response) {
  // console.log(response)
  if (response.data.code === 400) {
    location.href = '/login?to=/cart'
  }
  return response
}, function (error) {
  // Do something with response error
  return Promise.reject(error)
})

export default myaxios
